package com.abc.tacos.bean;

import lombok.Data;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * @author Kar
 * @create 2022-04-26 下午4:18
 */
@Data
public class RegistrationForm {

    @NotBlank(message = "username is required")
    private String username;
    @NotBlank(message = "password is required")
    private String password;
    @NotBlank(message = "confirm password is incorrect")
    private String confirmPassword;
    @NotBlank(message = "full name is required")
    private String fullname;
    // @NotNull
    private String street;
    // @NotNull
    private String city;
    // @NotNull
    private String state;
    // @NotNull
    private String zip;
    // @NotNull
    @Size(min = 8,max = 11,message = "length of phone number between 8 and 11")
    private String phone;

    public User toUser(PasswordEncoder passwordEncoder) {
        return new User(
                username, passwordEncoder.encode(password),
                fullname, street, city, state, zip, phone);
    }

    public void setPassword(String password) {
        this.password = password;
        checkPassword();//check
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
        checkPassword();//check
    }

    private void checkPassword() {
        //System.err.println("checkPassword");
        if(this.password == null || this.confirmPassword == null){
            return;
        }else if(!this.password.equals(confirmPassword)){
            this.confirmPassword = "";
        }
    }
}
